#define LAND_GRAPHICS_CORE

#include "init.h"

static struct{
	LandGraphics* config;
	LandWindowBackend* backend;
	LandWindow* root;
}ctx = {0};


int
landInitWindowContext(LandGraphics* config, LandWindowBackend* backend){
	ctx.config = config;
	ctx.backend = backend;
	return 0;
}

int
landExitWindowContext() {
	return 0;
}

LAND_GRAPHICS_API LandWindow*
landOpenWindow(const char* title, unsigned rect[4]) {
	size_t size = ctx.backend->sizeofWindow();
	LandWindow* window = malloc(size);
	window->ud = NULL;
	window->title[0] = '\0';
	window->next = NULL;
	window->x = rect[0];
	window->y = rect[1];
	window->width = rect[2];
	window->height = rect[3];
	window->onEvent = NULL;
	ctx.backend->openWindow(window);
	if (ctx.root) {
		ctx.root->next = window;
	}
	else {
		ctx.root = window;
	}
	return window;
}

LAND_GRAPHICS_API int
landCloseWindow(LandWindow* window) {
	return ctx.backend->closeWindow(window);
}

LAND_GRAPHICS_API void
landGetWindowSize(LandWindow* window, unsigned* w, unsigned* h) {
	*w = window->width;
	*h = window->height;
}

LAND_GRAPHICS_API void
landRenderWindow(LandWindow* window, LandCanvas* canvas) {
	ctx.backend->renderWindow(window, canvas);
}

LAND_GRAPHICS_API void
landOnWindowEvent(LandWindow* window,
	void (*onEvt)(LandWindow*, LandWindowEvent*)) {
	window->onEvent = onEvt;
}